package com.cxyd.modules.wms.controller;

import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wms.dto.PurchaseDTO;
import com.cxyd.modules.wms.service.PurchaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


/**
 * 入库主单
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/purchase")
public class PurchaseController {
    private final PurchaseService purchaseService;

    public PurchaseController(PurchaseService purchaseService) {
        this.purchaseService = purchaseService;
    }

    @GetMapping("page")
    @RequiresPermissions("wms:purchase:page")
    public Result<PageData<PurchaseDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<PurchaseDTO> page = purchaseService.page(params);

        return new Result<PageData<PurchaseDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:purchase:info")
    public Result<PurchaseDTO> get(@PathVariable("id") Long id) {
        PurchaseDTO data = purchaseService.get(id);

        return new Result<PurchaseDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:purchase:save")
    public Result save(@RequestBody PurchaseDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        purchaseService.save(dto);

        return new Result();
    }

//    @PostMapping
//    @LogOperation("Excel导入")
//    @RequiresPermissions("wms:purchase:file")
//    public Result importExcel(@RequestParam("file") MultipartFile excel) {
//        //效验数据
////        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
////        purchaseService.save(dto);
//
//        return new Result();
//    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:purchase:update")
    public Result update(@RequestBody PurchaseDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        purchaseService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:purchase:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "purchaseId");

        purchaseService.delete(ids);

        return new Result();
    }
}